package com.hugh.lagouserviceuser.util;

import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: Hugh
 * @Date: 2020/7/21
 */
public class Test {

    private static List<Set<String>> finalArrays = new ArrayList<>();


    private static Integer countSourPackge(String[][] arg){

//        List<List<String>> req
        List<String> allPackge = new ArrayList<>();
        Collections.addAll(allPackge,arg[0]);
        //获取第二个,先处理依赖关系
        for(int i=1;i<arg.length;i++){
            //[fiela,fielb]
            List<String> need2Check = Arrays.asList(arg[i]);
            Set<String> oldList = judgeContail(need2Check.get(0));
            if(isEmpty(oldList)){
                oldList = judgeContail(need2Check.get(1));
            }
            if(isEmpty(oldList)){
                //说明这个时候是个新的依赖包
                Set<String> newArray = new HashSet<>();
                newArray.add(need2Check.get(0));
                newArray.add(need2Check.get(1));
                finalArrays.add(newArray);
            }else{
                //说明有依赖关系
                oldList.add(need2Check.get(0));
                oldList.add(need2Check.get(1));
            }
            //移除总量里面的资源包
            if(allPackge.contains(need2Check.get(0))){
                allPackge.remove(need2Check.get(0));
            }
            if(allPackge.contains(need2Check.get(1))){
                allPackge.remove(need2Check.get(1));
            }
        }
        //判断是否有剩余
        if(allPackge.size()>0){
            for(String ss : allPackge){
                Set<String> set = new HashSet<>();
                set.add(ss);
                finalArrays.add(set);
            }
        }
        //返回size即可
        return finalArrays.size();
    }


    private static Set<String> judgeContail(String ss){
        for(Set<String> array : finalArrays){
            if(array.contains(ss)){
                return array;
            }
        }
        return Collections.emptySet();
    }
    public static boolean isEmpty( Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }

    public static void main(String[] args) {
        String[][] arg = {{"A","B","C","D","E"},{"A","B"},{"B","C"},{"D","E"}};

        Integer integer = countSourPackge(arg);

        String ss = "[12,12],[12,12],[12,12]";
        String[] split = ss.split(",");
        int i = Integer.parseInt("[1]");
        List<String> strings = Arrays.asList(split);
        List<List<Integer>> all = new ArrayList<>();
        System.out.println(strings);
    }
}
